package com.aurora.oa.work.view;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.R;
import com.aurora.oa.activity.BaseActivity;
import com.aurora.oa.adapter.CommonRecyclerViewAdapter;
import com.aurora.oa.adapter.CommonRecyclerViewHolder;
import com.aurora.oa.customview.ItemDivider;
import com.aurora.oa.listener.OnRequestAttendancePersonListener;
import com.aurora.oa.login.model.UserInfo;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/26 0026.
 */
public class AttendancePersonActivity extends BaseActivity{

    RequestHttpModel model = new RequestHttpModelImpl();
    private RecyclerView rv_sign_person;
    private CommonRecyclerViewAdapter<UserInfo> commonAdapter;
    GridLayoutManager gridLayoutManager;
    List<UserInfo> userList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitlebar(true,"出勤人数",true);
        setMyContentView(R.layout.activity_attendance_person);
        initView();
        getData();
    }

    private void initView() {
        gridLayoutManager = new GridLayoutManager(this,4);
        rv_sign_person = (RecyclerView) findViewById(R.id.rv_sign_person);
        commonAdapter = new CommonRecyclerViewAdapter<UserInfo>(this,userList) {
            @Override
            public void convert(CommonRecyclerViewHolder h, UserInfo entity, int position) {
                if(entity.issign==1){
                    h.setBackground(R.id.tv_attendance_statistic,R.drawable.bg_round_rectangle_blue);
                }else if(entity.issign==2){
                    h.setBackground(R.id.tv_attendance_statistic,R.drawable.bg_round_rectangle_darkgray);
                }
                h.setText(R.id.tv_title,entity.getRealname());
                h.setImageURL(R.id.my_image_view, AuroraConstants.BASE_SERVER_URL+"/"+entity.getHeadimg());

            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_attendance_person;
            }
        };
        commonAdapter.setOnViewInItemClickListener(new CommonRecyclerViewAdapter.OnViewInItemClickListener() {
            @Override
            public void onViewInItemClick(View v, int position) {
                Intent intent =new Intent(AttendancePersonActivity.this,AttendanceStatisticsActivity.class);
                intent.putExtra("id",userList.get(position).getId());
                startActivity(intent);
            }
        },R.id.tv_attendance_statistic);
        rv_sign_person.setAdapter(commonAdapter);
        rv_sign_person.setLayoutManager(gridLayoutManager);
        rv_sign_person.setItemAnimator(new DefaultItemAnimator());
        rv_sign_person.addItemDecoration(new ItemDivider().setDividerWith(6).setDividerColor(Color.TRANSPARENT));
    }

    private void getData() {
        model.getAttendancePerson(new OnRequestAttendancePersonListener() {
            @Override
            public void onRequestAttendancePersonSuccess(List<UserInfo> userinfoList) {
                    loadData(userinfoList);
            }

            @Override
            public void onRequestAttendancePersonFail(String message) {

            }
        });
    }

    private void loadData(List<UserInfo> userinfoList) {
        userList.clear();
        userList.addAll(userinfoList);
        commonAdapter.notifyDataSetChanged();
    }
}
